class RunningAccount < ApplicationRecord
  self.table_name = 'coupon_running_accounts'
  self.inheritance_column = false
  module UnionHack
    def union(*relations)
      from '((' + relations.map {|r| r.to_sql}.join(') UNION (') + ')) AS ' + self.table_name
    end
  end
  extend UnionHack

  enum status: [:pending, :refunded]
  scope :campaign, -> {
    RunningAccount.union(
        Transaction.joins(:campaigns_order, :campaigns_appointment).select(
            :id,
            :no,
            :order_no,
            "'Campaign' as order_type",
            "'爱动' as company_name",
            "'爱动' as store_name",
            'campaigns_appointments.campaign_name as product',
            :buyer,
            'campaigns_orders.total as total',
            'transactions.price as pay_amount',
            'campaigns_orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE campaigns_orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        )
    )
  }
  scope :course, -> {
    RunningAccount.union(
        Transaction.joins(:order, :company, :store, :appointment).select(
            :id,
            :no,
            :order_no,
            "'Order' as order_type",
            'companies.name as company_name',
            'stores.name as store_name',
            'appointments.course_cn_name as product',
            :buyer,
            'orders.total as total',
            'transactions.price as pay_amount',
            'orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        )
    )
  }
  scope :camp, -> {
    RunningAccount.union(
        Transaction.joins(:camp_order, :camp_store, :camp_appointment).select(
            :id,
            :no,
            :order_no,
            "'CampOrder' as order_type",
            "'爱动' as company_name",
            'stores.name as store_name',
            'camp_appointments.camp_name as product',
            :buyer,
            'camp_orders.total as total',
            'transactions.price as pay_amount',
            'camp_orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE camp_orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        )
    )
  }
  scope :coach, -> {
    RunningAccount.union(
        PayCallback.joins(:order, :appointment).select(
            :id,
            'serial_number as no',
            'pay_callbacks.order_id as order_no',
            "'CoachOrder' as order_type",
            "'爱动' as company_name",
            "'自营' as store_name",
            'training_appointments.training_name as product',
            'pay_callbacks.pay_account as buyer',
            'coach_orders.amount as total',
            'pay_callbacks.amount as pay_amount',
            "case coach_orders.pay_type when 1 then '支付宝' else '教练端-微信' end as pay_type",
            :status,
            :created_at,
            :updated_at,
            "CASE coach_orders.status
           WHEN 0 THEN pay_callbacks.created_at
           ELSE pay_callbacks.updated_at
         END as traded_at"
        )
    )
  }
  scope :boss, -> {
    RunningAccount.union(
        Boss::Transaction.joins(:order).select(
            :id,
            :no,
            :order_no,
            "'Boss::Order' as order_type",
            "'管家' as company_name",
            "'管家' as store_name",
            "CASE boss_orders.type WHEN 1 THEN '充值' ELSE CONCAT(boss_orders.schedule_date,' 课程付款') END AS product",
            :buyer,
            'boss_orders.total as total',
            'boss_orders.pay_amount as pay_amount',
            "boss_orders.pay_type  as pay_type",
            :status,
            :created_at,
            :updated_at,
            "boss_transactions.created_at as traded_at"
        )
    )
  }
  default_scope -> {
    RunningAccount.union(
        Transaction.joins(:campaigns_order, :campaigns_appointment).select(
            :id,
            :no,
            :order_no,
            "'Campaign' as order_type",
            "'爱动' as company_name",
            "'爱动' as store_name",
            'campaigns_appointments.campaign_name as product',
            :buyer,
            'campaigns_orders.total as total',
            'transactions.price as pay_amount',
            'campaigns_orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE campaigns_orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        ),
        Transaction.joins(:order, :company, :store, :appointment).select(
            :id,
            :no,
            :order_no,
            "'Order' as order_type",
            'companies.name as company_name',
            'stores.name as store_name',
            'appointments.course_cn_name as product',
            :buyer,
            'orders.total as total',
            'transactions.price as pay_amount',
            'orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        ),
        Transaction.joins(:camp_order, :camp_store, :camp_appointment).select(
            :id,
            :no,
            :order_no,
            "'CampOrder' as order_type",
            "'爱动' as company_name",
            'stores.name as store_name',
            'camp_appointments.camp_name as product',
            :buyer,
            'camp_orders.total as total',
            'transactions.price as pay_amount',
            'camp_orders.pay_type  as pay_type',
            :status,
            :created_at,
            :updated_at,
            "CASE camp_orders.status
           WHEN 0 THEN transactions.created_at
           ELSE transactions.updated_at
         END as traded_at"
        ),
        PayCallback.joins(:order, :appointment).select(
            :id,
            'serial_number as no',
            'pay_callbacks.order_id as order_no',
            "'CoachOrder' as order_type",
            "'爱动' as company_name",
            "'自营' as store_name",
            'training_appointments.training_name as product',
            'pay_callbacks.pay_account as buyer',
            'coach_orders.amount as total',
            'pay_callbacks.amount as pay_amount',
            "case coach_orders.pay_type when 1 then '支付宝' else '教练端-微信' end as pay_type",
            :status,
            :created_at,
            :updated_at,
            "CASE coach_orders.status
           WHEN 0 THEN pay_callbacks.created_at
           ELSE pay_callbacks.updated_at
         END as traded_at"
        ),
        Boss::Transaction.joins(:order).select(
            :id,
            :no,
            :order_no,
            "'Boss::Order' as order_type",
            "'管家' as company_name",
            "'管家' as store_name",
            "CASE boss_orders.type WHEN 1 THEN '充值' ELSE CONCAT(boss_orders.schedule_date,' 课程付款') END AS product",
            :buyer,
            'boss_orders.total as total',
            'boss_orders.pay_amount as pay_amount',
            "boss_orders.pay_type  as pay_type",
            :status,
            :created_at,
            :updated_at,
            "boss_transactions.created_at as traded_at"
        )
    )
  }
end

